#include<stdio.h>
#include<stdlib.h>
#include<string.h>

/* function malloc wrong (page48)
void GetMemory1(char *p,int num){
	p=(char *)malloc(sizeof(char)*num);
}

void Test1(){
	char *test1_p=NULL;
	GetMemory(test1_p,100); 
	strcpy(test1_p,"helen-test1"); //test1_p still NULL, 
	printf("test1_p=%s\n",test1_p);
}
*/
/* method1:function malloc right (page 48)*/
void GetMemory1(char **p,int num){
	*p=(char *)malloc(sizeof(char)*num);
}

void Test1(){
	char *test1_p=NULL;
	GetMemory1(&test1_p,100);//it is &test1_p not test1_p 
	strcpy(test1_p,"helen-test1");  
	printf("test1_p=%s\n",test1_p);
	free(test1_p);
}

/* method2:function malloc right (page 48)*/
char *GetMemory2(int num){
	char *p=(char *)malloc(sizeof(char) *num);
	return p;
}

void Test2(){
	char *test2_p=NULL;
	test2_p=GetMemory2(100);
	strcpy(test2_p,"helen-test2");  
	printf("test2_p=%s\n",test2_p);
	free(test2_p);
}
/*----------Test3 is not a good way(page 49)-----*/
char *getstring()
{
	char string1[]="helen-getstring";
	return string1;
}

void Test3(){
	char *get_s=NULL;
	get_s=getstring();
	printf("test3 get string:%s\n",get_s);
}
/*-----------Test3 end---------------------------------*/

/*page 49, even though it is not wrong, but string4 is a constant ,cannot change*/
char *getstring4()
{
	char *string4="helen-getstring4";
	return string4;
}

void Test4()
{
	char *get_s4=NULL;
	get_s4=getstring4();
	printf("test4 get string:%s\n",get_s4);
}
/*--------------------Test4 end------------------------*/

void Test5()
{
	printf("int sizeof:%d\n",(int)sizeof(int));
	printf("char sizeof:%d\n",(int)sizeof(char));
	printf("unsigned long sizeof:%d\n",(int)sizeof(unsigned long));
	printf("float sizeof:%d\n",(int)sizeof(float));
	printf("double sizeof:%d\n",(int)sizeof(double));
	printf("void * sizeof:%d\n",(int)sizeof(void *));
}

/*-------------Test6 will show invoid pointer,why ?----
void Test6()
{
	char *strd=NULL;
	char *strs="helen.ao";
	int str_l=strlen(strs);
	strd=(char *)malloc(sizeof(char)*(str_l+1));
	if(strd ==NULL)
		exit(1);
	strd=strs;
	printf("strd is %s\n",strd);
	free(strd);
	strd=NULL;
}
----------------Test6 end--------------*/

void Test6()
{
	char *strd=NULL;
	char *strs="helen.ao-helen_world";
	strd=strs; //point to the same address
	printf("2strd sizeof:%d,strs sizeof:%d\n",(int)sizeof(strd),(int)sizeof(strs));
	printf("2strd strlen:%d,strs strlen:%d\n",(int)strlen(strd),(int)strlen(strs));
	if(strd==strs)
		printf("strd is %s\n",strd);
}

void Fun1(char a[100]){
	//not 100, when array as a function parameter, it will change to a point auto, so 
	//the sizeof is pointer sizeof, 64bit os is 8 and 32bit os is 4
	printf("sizeof a:%d\n",(int)sizeof(a));
}

int main(int argc,char argv[])
{
	char a[]="helen";
	printf("string:%s,strlen:%d and sizeof:%d\n",a,(int )strlen(a),(int)sizeof(a));
	a[0]='X'; 
	printf("a=%s\n",a);
	char *p1="world"; //p point to constant string 
	//64-bit OS:sizeof(p)=8
	//32-bit OS:sizeof(p)=4
	printf("string:%s,strlen:%d and sizeof:%d\n",p1,(int )strlen(p1),(int)sizeof(p1));
	printf("p1=%s\n",p1);
	/* wrong
	 * p1[0]='X';// want to change constant string, so run error,
	 *printf("p1=%s\n",p1);
	 */
	char b[10]={0};
	strcpy(b,a);
	if(strcmp(b,a)==0)
		printf("string a the same as b\n");
	int len=strlen(a);
	char *p=(char *)malloc(sizeof(char)*(len+1));
	if(NULL==p){
		printf("pointer mallco fail");
		exit(1);
	}
	strcpy(p,a);
	if(strcmp(p,a)==0)
		printf("pinter p content string is the same as a\n");
	free(p);
	p=NULL;
	char a2[]="helen world";
	char *p2=a2;//p2 point to a2 address
	//64-bit OS:sizeof(p)=8
	//32-bit OS:sizeof(p)=4
	printf("a2=%s,p2=%s,a2_sizeof=%d,p2_sizeof=%d\n",a2,p2,(int)sizeof(a2),(int)sizeof(p2));
	char a3[64]="hi,helen";
	Fun1(a3);
	Test1();
	Test2();
	Test3();
	Test4();
	Test5();
	Test6();
	return 1;
}

